package com.example.xunua_weather.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xunua_weather.R;

import java.util.List;

public class DeleteCityAdapter extends BaseAdapter {
    Context context;
    List<String>cityList;
    List<String>deleteCity;

    public DeleteCityAdapter(Context context, List<String> cityList, List<String> deleteCity) {
        this.context = context;
        this.cityList = cityList;
        this.deleteCity = deleteCity;
    }

    @Override
    public int getCount() {
        return cityList.size();
    }

    @Override
    public Object getItem(int position) {
        return cityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_deletecity,null);
            holder=new ViewHolder();
            holder.tv=convertView.findViewById(R.id.item_delete_tv);
            holder.iv=convertView.findViewById(R.id.item_delete_iv);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        final String city=cityList.get(position);
        holder.tv.setText(city);
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cityList.remove(city);
                deleteCity.add(city);
                DeleteCityAdapter.this.notifyDataSetChanged();
            }
        });
        return convertView;
    }
    private class ViewHolder{
        ImageView iv;    //删除按钮
        TextView tv;    //显示城市
    }
}
